﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Web;
using System.Web.Mvc;
using Data;
using Web.Filters;
using Models;

namespace Web.Controllers
{
    public class HomeController : BaseController
    {
        //
        // GET: /Home/

        [HttpGet]
        [UpdateLastVisit]

        public ActionResult Index(int? page,int? id = null)
        {
           
            var dbNews = new NewsData();
            
            if (id != null)
            {
                var newsList = dbNews.GetNews(Convert.ToInt32(id));
                
                if (newsList != null)
                {
                    var dbComments = new CommentsData();
                    var commentsList = dbComments.GetComments((int)id);
                    if (commentsList != null)
                    {
                        var pager = new Pager(commentsList.Count, Request.Url.LocalPath, page);
                        if (pager.Check() == null)
                        {
                            pager.CurrentPage = 1;
                        }
                        ViewBag.pager = pager;
                    }
                    var oneNews = new OneNewsWithComments
                                      {
                                          Comments = commentsList,
                                          News = newsList[0]
                                      };
                    var authTicket = GetFormsAuthenticationTicket();
                    if (authTicket != null)
                    {
                        ViewBag.VoteOrNot = dbNews.VoteOrNot(authTicket.Name, Convert.ToInt32(id));
                    }
                    else
                        ViewBag.VoteOrNot = false;
                    return View("oneNews",  oneNews);
                }
                ViewBag.VoteOrNot = false;
                return View("oneNews");
            }
            else
            {
                var newsList = dbNews.GetNews();

                if (newsList != null)
                {
                    var pager = new Pager(newsList.Count, Request.Url.LocalPath, page);
                    if (pager.Check() == null)
                    {
                        Redirect(Request.Url.LocalPath);
                    }
                    ViewBag.pager = pager;
                }

                return View("News", newsList);
            }
        }

        [UserAuth]
        [HttpPost]
        [UpdateLastVisit]
        
        public ActionResult Index(int? page, int? rating,int id, string login, OneNewsWithComments oneNewsWithComments = null)
        {
            var dbComments = new CommentsData();
            var dbNews = new NewsData();
           
            if (oneNewsWithComments != null)
            {
                var addComment = oneNewsWithComments.AddComment;
                if (addComment != null)
                {
                    dbComments.AddComment(addComment);
                    var commentsList = dbComments.GetComments(id);
                    if (commentsList != null)
                    {
                        var pager = new Pager(commentsList.Count, Request.Url.LocalPath, page);
                        if (pager.Check() == null)
                        {
                            pager.CurrentPage = 1;
                        }
                        ViewBag.pager = pager;
                    }
                }
            }
            if (rating != null)
            {
                dbNews.SetRating((int) rating, id, login);
            }

            var oneNews = new OneNewsWithComments
                              {
                                  Comments = dbComments.GetComments(id),
                                  News = dbNews.GetNews(id)[0]
                              };
            var authTicket = GetFormsAuthenticationTicket();
            if (authTicket != null)
            {
                ViewBag.VoteOrNot = dbNews.VoteOrNot(authTicket.Name, id);
            }
            else
                ViewBag.VoteOrNot = false;
            
            return View("oneNews",oneNews);
        }

        [UserAuth]
        [UpdateLastVisit]
        
        public ActionResult AddNews()
        {
            return View("addNewsByUser");
        }

        [UserAuth]
        [HttpPost]
        [UpdateLastVisit]

        public ActionResult AddNews(News model, string tags, IEnumerable<HttpPostedFileBase> uploadFiles)
        {
            var tagsStrings = tags.Split(',');

            var dbNews = new NewsData();
            var id = dbNews.AddNews(model);

            foreach (string tag in tagsStrings)
            {
                dbNews.AddTagsBind(Convert.ToInt32(tag), id);
            }


            if (uploadFiles != null)
            {
                var path = AppDomain.CurrentDomain.BaseDirectory;
                var imagePath = ConfigurationManager.AppSettings["NewsImageUrl"];
                path = path + imagePath + Convert.ToString(id);

                Directory.CreateDirectory(path);
                foreach (var file in uploadFiles)
                {
                    if (file == null) continue;
                    if (file.ContentType != "image/jpeg" && file.ContentType != "image/png") continue;

                    var filename = Path.GetFileName(file.FileName);
                    if (filename == null) continue;

                    file.SaveAs(Path.Combine(path + "/", filename));

                    var dbImages = new ImagesData();
                    dbImages.SetNewsImageBind(id, file.FileName);
                }
            }

            ViewBag.Message = "The news was successfully added";
            ViewBag.newsID = id;
            return View("addNewsByUser");
        }

    }
    
}
